package ua.org.extjsbuilder.parser.runtime.data;

import ua.org.extjsbuilder.parser.JSObject;
import ua.org.extjsbuilder.parser.JSParser;
import ua.org.extjsbuilder.parser.runtime.NumberLiteralToken;

public class JSNumber implements JSObject {
	private double value;

	public JSNumber(int value) {
		this.value = value;
	}
	
	public JSNumber(double value) {
		this.value = value;
	}

	public JSNumber(NumberLiteralToken literal) {
		try {
			this.value = Double.parseDouble(literal.toString());
		}
		catch(NumberFormatException ex){
			JSParser.getParser().getErrorHandler()
				.error("Wrong number format",literal.getLine());
		}
	}

	@Override
	public boolean equals(Object obj) {
		if(obj==null)return value==0;
		if(obj==this)return true;
		if(obj instanceof Number){
			return ((Number)obj).doubleValue()==value;
		}
		if(obj instanceof JSNumber){
			return ((JSNumber)obj).value==value;
		}
		return false;
	}

	@Override
	public int hashCode() {
		return (int)(value%Integer.MAX_VALUE);
	}

	@Override
	public String toString() {
		return String.valueOf(value);
	}
}
